package com.tianyuan.lims.utils;

import com.alibaba.fastjson.JSON;
//import com.tianyuan.lims.dms.entity.lims.LimsResponse;
//import com.tianyuan.lims.dms.entity.lims.Yewufenpei;
import okhttp3.*;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class LimsHttpUtils {

    public static final String defaultToken = "defaultToken";

    public static Map<String, String> doPostJson(String url, String jsonData, String token){
        Map<String, String> result = new HashMap<>();
        //String jsonData = "{\"name\": \"lizr\"}";
        OkHttpClient client = new OkHttpClient();
        Request request = new Request.Builder()
                //.url("http://www.example.com/api/resource")
                .url(url)
                .post(RequestBody.create(jsonData.getBytes(), MediaType.parse("application/json")))
                .header("token", "qweqweasd123123123")
                .build();

        try {
            Response response = client.newCall(request).execute();
            String responseBody = "";
            int code = 0;
            String message = "";
            if (response.isSuccessful()) {
                code = response.code();
                 message =response.message();
                responseBody = response.body().string();
            } else {
                code = response.code();
                message =response.message();
            }
            result.put("code", code + "");
            result.put("message", message);
            result.put("body", responseBody);

        } catch (IOException e) {
            e.printStackTrace();
            result.put("code", "500");
            result.put("message", e.getMessage());
            result.put("body", "");
        }
        return result;
    }

    public static Map<String, String> doPost(String url, Map<String, String> params, String token){
        Map<String, String> result = new HashMap<>();
        OkHttpClient client = new OkHttpClient();
        FormBody.Builder formBuilder = new FormBody.Builder();
        for (Map.Entry<String, String> entry : params.entrySet()) {
            formBuilder.add(entry.getKey(), entry.getValue());
        }

        RequestBody formBody = formBuilder.build();
        Request request = new Request.Builder()
                .url(url)
                .post(formBody)
                .header("token", "qweqweasd123123123")
                .build();

        try {
            Response response = client.newCall(request).execute();
            String responseBody = "";
            int code = 0;
            String message = "";
            if (response.isSuccessful()) {
                code = response.code();
                message =response.message();
                responseBody = response.body().string();
            } else {
                code = response.code();
                message =response.message();
            }
            result.put("code", code + "");
            result.put("message", message);
            result.put("body", responseBody);

        } catch (IOException e) {
            e.printStackTrace();
            result.put("code", "500");
            result.put("message", e.getMessage());
            result.put("body", "");
        }
        return result;
    }

    public static void main(String[] args) {

//        Map<String, String> params = new HashMap<>();
//        params.put("stationId", "jiangxi");
//        params.put("yewufenpeiIds", "1,2,3");
//        //Map<String, String>  result = doPost("http://lims.tyjishu.com/monitoring/e"+"/yewufenpeistatus", params, "token");
//        Map<String, String> result = doPost("http://127.0.0.1:8700/monitoring/e"+"/yewufenpeistatus", params, "token");
//        result.forEach((k,v)->{
//            System.out.println(k + ":" + v);
//        });
//        if(result.get("code") != null && result.get("code").equals("200")){
//            LimsResponse response = JSON.parseObject(result.get("body"), LimsResponse.class);
//            List<Yewufenpei> yewufenpeis =response.getRet().getYewufenpeis();
//            for(Yewufenpei yfp:yewufenpeis){
//                System.out.println(yfp.getId() + ":" + yfp.getYwfpstatus());
//            }
//        }







    }
}
